using System.Security.Cryptography;

namespace FileTransfer.Client.Utils;

public static class Md5
{
    public static string CalcFile(Stream stream)
    {
        using var md5 = MD5.Create();
        var hash = md5.ComputeHash(stream);
        stream.Position = 0; // Reset position after reading
        return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
    }

    public static string CalcFile(FileInfo file)
    {
        using var stream = file.OpenRead();
        return CalcFile(stream);
    }

    public static string CalcBytes(byte[] bytes)
    {
        using var md5 = MD5.Create();
        var hash = md5.ComputeHash(bytes);
        return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
    }
}
